/*    */ package com.zimbra.cs.mailclient.util;
/*    */ 
/*    */ import java.io.IOException;
/*    */ import java.io.OutputStream;
/*    */ import java.nio.ByteBuffer;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class ByteBufferOutputStream
/*    */   extends OutputStream
/*    */ {
/*    */   private ByteBuffer buf;
/*    */   
/*    */   public ByteBufferOutputStream(int size)
/*    */   {
/* 27 */     this.buf = ByteBuffer.allocate(size);
/*    */   }
/*    */   
/*    */   public ByteBufferOutputStream() {
/* 31 */     this(32);
/*    */   }
/*    */   
/*    */   public void write(int b) throws IOException {
/* 35 */     ensureCapacity(1);
/* 36 */     this.buf.put((byte)b);
/*    */   }
/*    */   
/*    */   public void write(byte[] b, int off, int len) throws IOException {
/* 40 */     if (len == 0) {
/* 41 */       return;
/*    */     }
/* 43 */     if ((off < 0) || (off > b.length) || (len < 0) || (off + len > b.length) || (off + len < 0))
/*    */     {
/* 45 */       throw new IndexOutOfBoundsException();
/*    */     }
/* 47 */     ensureCapacity(len);
/* 48 */     this.buf.put(b, off, len);
/*    */   }
/*    */   
/*    */   private void ensureCapacity(int count) throws IOException {
/* 52 */     if (this.buf.remaining() < count) {
/* 53 */       int size = Math.max(this.buf.capacity() * 2, this.buf.capacity() + count);
/* 54 */       if (size < 0) {
/* 55 */         throw new IOException("buffer limit exceeded");
/*    */       }
/* 57 */       ByteBuffer tmp = ByteBuffer.allocate(size);
/* 58 */       this.buf.flip();
/* 59 */       tmp.put(this.buf);
/* 60 */       this.buf = tmp;
/*    */     }
/*    */   }
/*    */   
/*    */   public ByteBuffer getByteBuffer() {
/* 65 */     return this.buf;
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/util/ByteBufferOutputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */